package com.lsw.redisspringboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsw.redisspringboot.entity.EduTeacher;
import com.lsw.redisspringboot.service.EduTeacherService;
import com.lsw.redisspringboot.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/eduservice/teacher")
@Api( "讲师管理")
@CrossOrigin
public class EduTeacherController {

    public EduTeacherController(EduTeacherService eduTeacherService) {
        this.eduTeacherService = eduTeacherService;
    }

    //@Autowired
    private EduTeacherService eduTeacherService;


    // 查询讲师列表
    @ApiOperation("查询讲师列表")
    @GetMapping("findAll")

    public R findAllTeachers() {
        List<EduTeacher> list = eduTeacherService.findAllTeachers();
        return R.success().data("teacherList", list);
    }
    @CachePut(value="eduTeacher",key = "'teacherList'")
    // 添加讲师
    @PostMapping("addTeacher")
    @ApiOperation("添加讲师")
    public R addTeacher(@ApiParam(name = "eduTeacher", value = "讲师对象，gmtCreate和gmtModified自动填充，id自动生成，这三项不需要传值", required = false)
                        @RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.addTeacher(eduTeacher);

        if (save) {
            return R.success();
        }else {
            return R.error();
        }
    }

    // 逻辑删除讲师
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师id", required = true)
                                   @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.success();
        } else {
            return R.error();
        }
    }

    // 修改讲师信息
    @ApiOperation("修改讲师信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(@ApiParam(name = "eduTeacher", value = "需要修改的讲师信息，gmtCreate和gmtModified自动填充,这两项不需要传值", required = false)
                           @RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.success();
        }else {
            return R.error();
        }
    }
    // 分页查询讲师
    /*
     *  方法参数：
     *  current 当前页
     *  limit 每页记录数
     */
    /*@ApiOperation(value = "分页查询讲师列表",response = EduTeacher.class,responseContainer = "Map")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        // 调用分页方法
        // 调用方法时，mp底层会把分页得到的所有数据封装到pageTeacher中
        eduTeacherService.page(pageTeacher, null);


         //模拟产生异常
        try {
            int i = 1/0;
        }catch (Exception e){
            throw new RuntimeException("执行了自定义异常处理");
        }


        long total = pageTeacher.getTotal();// 总记录数
        List<EduTeacher> records = pageTeacher.getRecords();// 数据list集合

        // 两种返回形式
        // 第一种
        *//*
        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return ReturnResult.success().data(map);
        *//*

        // 第二种
        return R.success().data("total", total).data("rows", records);
    }*/


    // 条件查询带分页
    // @RequestBody 代表从前端接收json类型的参数，自动封装到teacherQuery对象中
    /*@ApiOperation("条件查询带分页查询讲师列表")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页", required = true)
                                                 @PathVariable long current,
                                  @ApiParam(name = "limit", value = "每页结果数", required = true)
                                                 @PathVariable long limit,
                                  @ApiParam(name = "teacherQuery", value = "查询条件")
                                                 @RequestBody(required = false) TeacherQuery teacherQuery) {
        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        // 构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件动态查询   动态sql拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String beginTime = teacherQuery.getBeginTime();
        String endTime = teacherQuery.getEndTime();
        // 判断条件是否为空，不为空则拼接
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(beginTime)) {
            // wrapper.ge代表大于等于
            wrapper.ge("gmt_create", beginTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            // wrapper.le代表小于等于
            wrapper.le("gmt_create", endTime);
        }

        wrapper.orderByDesc("gmt_create");

        // 掉用分页方法
        eduTeacherService.page(pageTeacher, wrapper);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.success().data("total", total).data("rows", records);
    }*/



    // 根据id查询讲师信息
   /* @ApiOperation("根据id查询讲师信息")
    @GetMapping("findTeacherById/{id}")
    public R findTeacherById(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.success().data("teacher",eduTeacher);
    }
*/

}